package com.ssm.dao;

import com.ssm.domain.Role;
import com.ssm.domain.SysFile;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 文件数据操作接口
 */
public interface IFileDao {

    /**
     * 查询所有文件
     * @return
     * @throws Exception
     */
    @Select("select * from sys_file order by uploadtime desc")
    List<SysFile> findAll() throws Exception;

    /**
     * 删除文件以文件夹中的文件信息
     * @param fileId
     */
    @Delete("delete from file_folder where fileid=#{fileId}")
    void deleteFileformFolder(String fileId);
    @Delete("delete from sys_file where id=#{fileId}")
    void deleteFileByFileId(String fileId);

    /**
     * 上传文件
     * @param sysFile
     */
    @Insert("insert into sys_file(filename,filesize,filetype,filepath,uploadtime,username) values(#{filename},#{filesize},#{filetype},#{filepath},#{uploadtime},#{username})")
    void fileInsert(SysFile sysFile);

    /**
     * 根据id查找文件
     * @param fileId
     * @return
     */
    @Select("select * from sys_file where id=#{fileId}")
    SysFile selectOne(@Param("fileId")String fileId);

    /**
     * 根据id查找文件
     * 当数据库字段名与实体诶对应属性不一致时，使用@Result映射来将其对应
     *   column字段名，property实体类属性名，javaType为数据库字段数据类型
     * 使用@Many注解实现一对多查询
     * @param fileId
     * @return
     */
    @Select("select * from sys_file where id=#{fileId}")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "filename", column = "filename"),
            @Result(property = "filesize", column = "filesize"),
            @Result(property = "filepath", column = "filepath"),
            @Result(property = "filetype", column = "filetype"),
            @Result(property = "uploadtime", column = "uploadtime"),
            @Result(property = "username", column = "username"),
            @Result(property = "folders",column = "id",javaType = java.util.List.class,many = @Many(select = "com.ssm.dao.IFolderDao.findFolderByFileId"))
    })
    SysFile findById(String fileId);

    /**
     * 根据文件夹id查询文件
     * @param folderId
     * @return
     * @throws Exception
     */
    @Select("select * from sys_file where id in (select fileId from file_folder where folderId =#{folderId})")
    List<SysFile> findFileByFolderId(String folderId) throws Exception;

    /**
     * 根据keyword查询文件
     * @param keyWord
     * @return
     */
    List<SysFile> selectFiles(@Param("keyWord") String keyWord);

}
